﻿using _2112A_2_MES._XiangHe;
using _2112A_2_MES.ChuBoWens;
using FourGroups.Project;
using Microsoft.AspNetCore.Mvc;
using Microsoft.IdentityModel.Tokens;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Volo.Abp.Application.Services;
using Volo.Abp.Domain.Repositories;

namespace _2112A_2_MES.ChuBoWen
{
    /// <summary>
    /// 库存现有量
    /// </summary>
    [ApiExplorerSettings(GroupName = "楚博文")]
    public class ProductEntryFormService: ApplicationService, IProductEntryFormService
    {
        /// <summary>
        /// 客户仓储
        /// </summary>
        private readonly IRepository<ProductEntryForm, int> _productEntryFormRepository;
        public ProductEntryFormService(IRepository<ProductEntryForm, int> productEntryFormRepository)
        {
            _productEntryFormRepository = productEntryFormRepository;
        }

        /// <summary>
        /// 获取库存现有量列表
        /// </summary>
        /// <param name="itemname"></param>
        /// <param name="warehousename"></param>
        /// <param name="expiredate"></param>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <returns></returns>
        public async Task<InterSelect<object>> GetProductEntryFormList(string? itemname, string? warehousename, string? expiredate, int pageIndex, int pageSize)
        {
            var productentryformList = await _productEntryFormRepository.GetListAsync();
            if (!string.IsNullOrEmpty(itemname))
            {
                productentryformList = productentryformList.Where(p => p.ItemName.Contains(itemname)).ToList();
            }
            if (!string.IsNullOrEmpty(warehousename))
            {
                productentryformList = productentryformList.Where(p => p.WarehouseName.Contains(warehousename)).ToList();
            }
            if (!string.IsNullOrEmpty(expiredate))
            {
                productentryformList = productentryformList.Where(p => p.ExpireDate==DateTime.Parse(expiredate)).ToList();
            }
            var totalCount = productentryformList.Count;
            var pageList = productentryformList.Skip((pageIndex - 1) * pageSize).Take(pageSize).ToList();
            var productClassificationList = AutoMapperExt.MapToList<ProductEntryForm>(pageList);
            var data = new { TotalCount = totalCount, PageList = productClassificationList };
            if (productentryformList != null)
            {
                return new InterSelect<object>(true, "0", data);
            }
            return new InterSelect<object>(false, "1", null);
        }

        /// <summary>
        /// 新增库存现有量
        /// </summary>
        /// <param name="productentryform"></param>
        /// <returns></returns>
        public async Task<int> AddProductEntryForm(ProductEntryForm productentryform)
        {
            var result = await _productEntryFormRepository.FindAsync(x => x.ItemCode == productentryform.ItemCode);
            if (result == null)
            {
                productentryform.CreateBy = "楚博文";
                productentryform.CreateTime = DateTime.Now;
                await _productEntryFormRepository.InsertAsync(productentryform);
                return 1;
            }
            return 0;
        }

        /// <summary>
        /// 删除库存现有量
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public async Task<int> DeleteProductEntryForm(int id)
        {
            await _productEntryFormRepository.DeleteAsync(id);
            return 1;
        }

        /// <summary>
        /// 获取库存现有量
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public async Task<ProductEntryForm> GetProductEntryForm(int id)
        {
            var productentryform = await _productEntryFormRepository.GetAsync(id);
            return productentryform;
        }

        /// <summary>
        /// 修改库存现有量
        /// </summary>
        /// <param name="productentryform"></param>
        /// <returns></returns>
        public async Task<int> UpdateProductEntryForm(ProductEntryFormDTO productentryform)
        {
            var result = await _productEntryFormRepository.GetAsync(productentryform.Id);
            result.ItemCode= productentryform.ItemCode;
            result.ItemName = productentryform.ItemName;
            result.SpecType = productentryform.SpecType;
            result.StockNum = productentryform.StockNum;
            result.Units = productentryform.Units;
            result.BatchCode = productentryform.BatchCode;
            result.WarehouseName = productentryform.WarehouseName;
            result.LocationName = productentryform.LocationName;
            result.AreaName = productentryform.AreaName;
            result.ExpireDate = productentryform.ExpireDate;
            result.UpdateBy = "小呆呆";
            result.UpdateTime = DateTime.Now;
            await _productEntryFormRepository.UpdateAsync(result);
            return result != null ? 1 : 0;
        }
    }
}
